<?php

// 管理员表

namespace app\console\model;
use think\Db;
use think\Model;
use think\Session;
use think\Request;

class Manager extends Model
{

    // 模型事件
    protected static function init()
    {
        // 新增前
        self::event('before_insert', function ($data) {

            $data->create_ip = getIP();
            $data->create_time = date('Y-m-d H:i:s', time());

        });

        // 写入前
        self::event('before_write', function ($data) {

            $data->login_ip = getIP();
            $data->update_time = date('Y-m-d H:i:s', time());
            $data->password = self::passwordAttr(Request::instance()->param('password'), Request::instance()->param('id'));

        });
        self::event('after_insert', function ($data) {
            if($data->level == 4){
                Db::name('salesman')->insert(['parent_id' => $data->parent_id , 'manage_id' => $data->id]);
            }

        });
//        self::event('after_update', function ($data) {
//            if($data->level == 4){
//                Db::name('salesman')->where('manage_id',$data->id)->update(['parent_id' => $data->parent_id , 'manage_id' => $data->id]);
//            }
//
//        });
    }


    // 属性方法
    protected static function passwordAttr($password, $id)
    {
        if (empty($password) && !empty($id))
            return (new Manager())->where(['id' => $id])->value('password');
        else
            return mcs($password);
    }


    // 登录方法
    public static function login()
    {

        $map = [
            'username' => Request::instance()->post('username', '', 'strtolower'),
         'password' => Request::instance()->post('password', '', 'mcs'),
            'status'   => 1
        ];

        $find = self::find();

        // 用户名&密码 验证成功
        if ($find) {

            Session::set('manage_id', $find['id']);
            Session::set('manage_user', $find['username']);
            Session::set('manage_nick', $find['nickname']);
            Session::set('manage_avatar', '/static/console/images/' . $find['avatar'] . '.jpg');
            $manage_id = Db::name('manager')->where('id',Session::get('manage_id'))->value('level');
            $auding_status = Db::name('auth_group')->where('id',$manage_id)->value('auding_status');//默认1没有审核权限
            $shop_id = DB::name('shop')->where('shop_manage_id',Session::get('manage_id'))->value('id');

            Session::set('auding_status', $auding_status);
            Session::set('role_id', $manage_id);
            Session::set('shop_id', $shop_id);
            return true;
        }
        $param = Request::instance()->param();
        $find2 = (new ShopManager())->where(['username' => $param['username'],'password' => $param['password']])->find();

        if($find2){

            $shop_manage_id = (new \app\shop\model\Shop())->where('id',$find2['shop_id'])->value('shop_manage_id');
            $find = self::where('id',$shop_manage_id)->find();
            Session::set('manage_id', $find['id']);
            Session::set('manage_user', $find2['username']);
            Session::set('manage_nick', $find2['name']);
            Session::set('manage_avatar', '/static/console/images/' . $find['avatar'] . '.jpg');
            $manage_id = Db::name('manager')->where('id',Session::get('manage_id'))->value('level');
            $auding_status = Db::name('auth_group')->where('id',$manage_id)->value('auding_status');//默认1没有审核权限
            $shop_id = DB::name('shop')->where('shop_manage_id',Session::get('manage_id'))->value('id');

            Session::set('auding_status', $auding_status);
            Session::set('role_id', $manage_id);
            Session::set('shop_id', $shop_id);
            return true;
        }

        return false;
    }
}